import argparse
import re

def count_characters(file_path):
    with open(file_path, 'r', encoding='utf-8') as f:
        return len(f.read())

def count_words(file_path):
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()
        words = re.findall(r'\b[a-zA-Z0-9]+\b', content)
        return len(words)

def count_sentences(file_path):
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()
        sentences = re.split(r'[.!?]+', content)
        return len([s for s in sentences if s.strip()])

def count_code_lines(file_path):
    code_lines = 0
    with open(file_path, 'r', encoding='utf-8') as f:
        for line in f:
            line = line.strip()
            if line and not line.startswith('#'):
                code_lines += 1
        return code_lines

def count_empty_lines(file_path):
    empty_lines = 0
    with open(file_path, 'r', encoding='utf-8') as f:
        for line in f:
            if line.strip() == '':
                empty_lines += 1
        return empty_lines

def count_comment_lines(file_path):
    comment_lines = 0
    with open(file_path, 'r', encoding='utf-8') as f:
        for line in f:
            if line.strip().startswith('#'):
                comment_lines += 1
        return comment_lines

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='文本统计工具')
    parser.add_argument('file', help='要统计的文件路径')
    parser.add_argument('-c', action='store_true', help='统计字符数')
    parser.add_argument('-w', action='store_true', help='统计单词数')
    parser.add_argument('-s', action='store_true', help='统计句子数')
    parser.add_argument('-cl', action='store_true', help='统计代码行（Python）')
    parser.add_argument('-el', action='store_true', help='统计空行')
    parser.add_argument('-coml', action='store_true', help='统计注释行（Python）')

    args = parser.parse_args()

    if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("命令格式错误，正确格式：wc.exe [参数] [文件名]")
        print("参数说明：-c 统计字符数；-w 统计单词数；-s 统计句子数")
        sys.exit(1)

    if args.c:
        print(f"字符数: {count_characters(args.file)}")
    if args.w:
        print(f"单词数: {count_words(args.file)}")
    if args.s:
        print(f"句子数: {count_sentences(args.file)}")
    if args.cl:
        print(f"代码行: {count_code_lines(args.file)}")
    if args.el:
        print(f"空行: {count_empty_lines(args.file)}")
    if args.coml:
        print(f"注释行: {count_comment_lines(args.file)}")
